package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.TaxDto;
import com.cwd.arm.application.service.read.facade.TaxReadService;
import com.cwd.arm.application.service.write.facade.TaxWriteService;
import com.cwd.arm.inter.query.TaxQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.QueryToDaoQueryUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.tax.TaxAddVo;
import com.cwd.arm.inter.vo.tax.TaxUpdateVo;
import com.cwd.arm.inter.vo.tax.TaxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Description:开票信息
* Created on 2021年11月16日
* @author  yan
*/
@Api(value = "开票信息接口", description = "开票信息接口")
@RestController
@RequestMapping(value = "/tax")
public class TaxController {

    @Autowired
    private TaxReadService readService;

    @Autowired
    private TaxWriteService writeService;

    @ApiOperation(value = "查询开票信息信息", notes = "查询开票信息信息")
    @ApiImplicitParams(value = {
    @ApiImplicitParam(name = "id", value = "开票信息id",required = true,dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<TaxVo> getTax(@PathVariable Long id) {
        TaxDto dto = readService.selectById(id);
        return ResponseVo.<TaxVo>BUILDER().success(convertToVo(dto));
    }

    @ApiOperation(value = "查询开票列表", notes = "查询开票列表")
    @GetMapping("")
    public ResponseVo<List<TaxVo>> getTaxList(TaxQuery query) {
        List<TaxDto> taxDtoList = readService.selectByQuery(QueryToDaoQueryUtil.toDaoQuery(query));
        return ResponseVo.<List<TaxVo>>BUILDER().success(DtoToVoUtil.convertToTaxVoList(taxDtoList));
    }

    @ApiOperation(value = "新增开票", notes = "新增开票")
    @Secured({"ROLE_TAX_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody TaxAddVo addVo) {
        writeService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改开票", notes = "修改开票")
    @Secured({"ROLE_TAX_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody TaxUpdateVo updateVo) {
        writeService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除开票", notes = "删除开票")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "开票id",required = true,dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_TAX_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        writeService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }

    private TaxVo convertToVo(TaxDto dto){
        TaxVo vo = new TaxVo();
        BeanUtils.copyProperties(dto,vo);
        return vo;
    }

}
